//
// Created by 张亦乐 on 2018/10/24.
//
#include "FoodManager.hpp"

#include <sys/time.h>
#include "base/Timestamp.hpp"
#include "GameSetting.hpp"

void FoodManager::CreateFood()
{
    int64_t nows = Timestamp::Nowms();
    if (nows - _lastFreshtime < FOOD_REFRESH_INTERVAL)return;

    for (int i = 0; i < CREATE_FOOD_NUM; ++i)
    {
        float x = rand()%1000;
        float y = rand()%1000;
        _foodDcit[_uniqueId++] = Food{x,y};
    }
    _lastFreshtime = nows;
}

bool FoodManager::GetFood(FOOD_ID id,Food& out)
{
    auto iter = _foodDcit.find(id);
    if(iter != _foodDcit.end())
    {
        out = iter->second;
        return true;
    }
    return false;
}

bool FoodManager::EatFood(FOOD_ID id)
{
    auto iter = _foodDcit.find(id);
    if(iter != _foodDcit.end())
    {
        _foodDcit.erase(iter);
        return true;
    }
    return false;
}